package com.example.base_kotlin_xml

import com.example.base_model.test_model.UserOne
import com.example.base_model.test_model.UserTwo
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import org.junit.Test

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        val userOne = UserOne(name = "Alice", sex = "Female", age = 25, email = null)

        // 创建Moshi实例
        val moshi = Moshi.Builder()
            .addLast(KotlinJsonAdapterFactory())
            .build()

        // 获取UserOne和UserTwo的JsonAdapter
        val userOneAdapter = moshi.adapter(UserOne::class.java)
        val userTwoAdapter = moshi.adapter(UserTwo::class.java)

        // 将UserOne转换为JSON字符串
        val json = userOneAdapter.toJson(userOne)

        // 将JSON字符串转换为UserTwo对象
        val userTwo = userTwoAdapter.fromJson(json)

        println("moshi: $userTwo") // UserTwo(name=Alice, age=25, phone=null)
    }
}